package com.qa.service.impl;

import com.qa.dao.ArticleMapper;
import com.qa.dao.CategoryArticleMapper;
import com.qa.dao.CategoryArticleVoMapper;
import com.qa.dao.CategoryMapper;
import com.qa.pojo.Article;
import com.qa.pojo.Category;
import com.qa.pojo.CategoryArticle;
import com.qa.service.ICategoryArticleService;
import com.qa.vo.CategoryArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/3/12 21:49
 * @Desc
 **/
@Service
public class CategoryArticleServiceImpl implements ICategoryArticleService {
    @Autowired
    private CategoryArticleMapper categoryArticleMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryArticleVoMapper categoryArticleVoMapper;


    @Override
    public List<CategoryArticle> getByArticleId(Integer articleId) {
        return categoryArticleMapper.getByArticleId(articleId);
    }

    @Override
    public void add(String text) {
        String[] split = text.replace("{", "").replace("\"", "").split(",");
        Article article = articleMapper.selectByPrimaryKey(Integer.parseInt(split[0].split(":")[1]));
        int i = text.replace("{", "").replace("\"", "").indexOf(",");
        String substring = text.replace("{", "").replace("\"", "").substring(i + 1, text.replace("{", "").replace("\"", "").length());
        String[] strings = substring.split(",");
        for (String string : strings) {
            String[] split1 = string.split(":");
            CategoryArticle categoryArticle = new CategoryArticle();
            categoryArticle.setArticleid(article.getId());
            categoryArticle.setCaategoryName(split1[0]);
            Category category = categoryMapper.getByName(split1[0]);
            categoryArticle.setCategoryid(category.getId());
            categoryArticleMapper.insert(categoryArticle);
        }

    }

    @Override
    public List<CategoryArticleVo> getByCategoryId(Integer categoryId) {
        return categoryArticleVoMapper.getAllByStatus(categoryId);
    }
}
